<?php

function is_working_day($ref_date) {
	elgg_load_library('elgg:oci_access');
	$recordset = oci_query("select unique t_date from nav_sc where t_date=TO_DATE('$ref_date','yyyy-mm-dd')"); 	
	if(count($recordset)){ 
		return true;
	} else {
		return false;
	}
}

function last_working_day($ref_date) {
	return adjust_to_working_day($ref_date);
}

function last_working_day_of_last_week($ref_date) {
	elgg_load_library('elgg:oci_access');
	
	$recordset = oci_query("select TO_CHAR(trunc(TO_DATE('$ref_date','yyyy-mm-dd'),'D'), 'yyyy-mm-dd') from dual");
	return adjust_to_working_day($recordset[0][0]);
}

function last_working_day_of_last_month($ref_date) {
	throw new Exception('Not implemented.');
}

function last_working_day_of_last_year($ref_date) {
	elgg_load_library('elgg:oci_access');
	
	$recordset = oci_query("select TO_CHAR(trunc(TO_DATE('$ref_date','yyyy-mm-dd'),'Y'), 'yyyy-mm-dd') from dual");
	return adjust_to_working_day($recordset[0][0]);
}

function adjust_to_working_day($ref_date) {
	
	elgg_load_library('elgg:oci_access');
	
	$recordset = oci_query("select TO_CHAR(MAX(t_date), 'yyyy-mm-dd') from nav_sc where t_date<TO_DATE('$ref_date','yyyy-mm-dd')"); 
	if(count($recordset)){ 
		return $recordset[0][0];
	} else {
		throw new Exception('Can not get last working day from Database.');
	}
}